<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="700px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" inline label-width="170px" label-suffix=":" class="form-class" v-loading="loading">
            <el-form-item label="店铺" prop="shop_id">
                <el-select v-model="model.shop_id" placeholder="请选择店铺" clearable>
                    <el-option v-for="(dict, index) in shopList" :key="index" :label="dict.shop_name" :value="dict.shop_id" />
                </el-select>
            </el-form-item>
            <el-form-item label="使用类型" prop="type_id">
                <el-select v-model="model.type_id" placeholder="请选择使用类型" clearable>
                    <el-option v-for="(dict, index) in typeList" :key="index" :label="dict.label" :value="dict.value" />
                </el-select>
            </el-form-item>

            <el-form-item label="归属运营" prop="admin_id">
                <el-select v-model="model.admin_id" placeholder="请选择运营" clearable>
                    <el-option v-for="(item, index) in salesmanList" :key="index" :label="item.nickname" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="归属标签" prop="label">
                <el-input v-model.trim="model.label" placeholder="不填则是主运营" clearable></el-input>
            </el-form-item>
            <el-form-item label="开始时间" prop="createtime">
                <el-date-picker v-model="model.createtime" type="date" format="yyyy-MM-dd" placeholder="请选择开始时间" clearable> </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间" prop="updatetime">
                <el-date-picker v-model="model.updatetime" type="date" format="yyyy-MM-dd" placeholder="不填则代表至今在用" clearable> </el-date-picker>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { ascriptionAdd, ascriptionEdit, getShopList, getSalesmanList } from '@/api/shop'
import { parseTime } from '@/utils/quickadmin'
export default {
    name: 'AscriptionDetailDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            loading: false,
            save: false,
            model: {
                id: '',
                admin_id: '',
                shop_id: '',
                type_id: '',
                label: '',
                createtime: '',
                updatetime: '',
            },
            rules: {
                createtime: [{ required: true, message: '开始时间不能为空', trigger: ['blur', 'change'] }],
                // shop_name: [{ required: true, message: '供应商名称不能为空', trigger: ['blur', 'change'] }],
                // supply_link: [{ required: true, message: '供应商链接不能为空', trigger: ['blur', 'change'] }],
            },
            shopList: [],
            salesmanList: [],
            typeList: [
                { label: '单人使用', value: 1 },
                { label: '多人使用', value: 2 },
            ],
        }
    },
    computed: {
        title() {
            return `${this.model.id ? '编辑' : '新增'}店铺`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    this.model = Object.assign({}, this.model, val)
                    if(this.model.updatetime == "至今"){
                        this.model.updatetime = ""
                    }
                    
                }
            },
            immediate: true,
            deep: true,
        },
    },
    async created() {
        this.loading = true
        try {
            const shopData = await getShopList()
            this.shopList = shopData?.data?.list ?? []
            const salesmanData = await getSalesmanList()
            this.salesmanList = salesmanData?.data?.list ?? []
        } catch (error) {
            console.log(error)
        }
        this.loading = false
    },
    methods: {
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }

                const params = { 
                    ...this.model,
                    createtime: parseTime(this.model.createtime, 'y-m-d'),
                    updatetime: parseTime(this.model.updatetime, 'y-m-d'),
                 }
                if (this.save) {
                    return
                }
                this.save = true
                const request = this.model.id ? ascriptionEdit : ascriptionAdd
                request(params)
                    .then(res => {
                        this.$message.success(res.msg)
                        this.dialogVisible = false
                        this.$emit('success')
                    })
                    .finally(() => {
                        this.save = false
                    })
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style>
.form-class.el-form .el-form-item {
    width: 100%;
    margin-right: 0;
}
.form-class.el-form .el-form-item .el-input {
    width: 400px;
}
</style>
